﻿using Microsoft.AspNetCore.Mvc;
using SiteApp.BLL.Interface;
using SiteApp.Web.Model;
using System.Linq;
using System.Threading.Tasks;

namespace SiteApp.Web.Controllers
{
    [ApiController]
    [Route("[controller]")]
    /// <summary>
    /// 与电站相关接口
    /// </summary>
    /// <returns></returns>
    public class SiteController : ControllerBase
    {
        private bool IsValid()
        {
            if (HttpContext.User.Claims.ToList() != null && HttpContext.User.Claims.ToList()[0].Type == "IsAuthenticated" && HttpContext.User.Claims.ToList()[0].Value == "true")
            {
                return true;
            }
            return false;
        }


        private readonly ISiteService siteService = null;
        private readonly IPointService pointService = null;

        public SiteController(
             ISiteService site, IPointService point)
        {
            this.siteService = site;
            this.pointService = point;
        }


        /// <summary>
        /// 用户站点查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("UserSite")]
        public async Task<JsonResult> UserSite([FromForm] string userId)
        {
         
            var result = new WebResult();

            if (IsValid())
            {
                result.Data = siteService.UserSite(userId).Select(a => new {
                    count = a.SiteId.Distinct().Count(),
                    point = a.SID.ToString().Distinct().Count()
                }).FirstOrDefault();
            }

            return new JsonResult(result);
        }



        /// <summary>
        /// 用户报警列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("Police")]
        public async Task<JsonResult> Police([FromForm] string userId)
        {

            var result = new WebResult();

            if (IsValid())
            {
                result.Data = siteService.Police(userId);
            }



            return new JsonResult(result);
        }









    }
}
